package ttOrder.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ttOrder.dao.FoodMapper;
import ttOrder.model.Food;
import ttOrder.model.FoodType;
import ttOrder.vo.FoodAndType;
import ttOrder.vo.FoodTypeVO;
import ttOrder.vo.FoodVO;

@Transactional
@Service
public class FoodService implements IFoodService{

	@Autowired
	public FoodMapper foodMapper;
	
	//查询全部菜品
	@Override
	public List<FoodVO> findAllFood() {
		
		List<FoodVO> vo = new ArrayList<FoodVO>() ;
		List<Food> food = foodMapper.selectAll();
		for(Food f:food){
			FoodVO foodVO = new FoodVO();
			BeanUtils.copyProperties(f, foodVO);
			FoodTypeVO foodTypeVO = new FoodTypeVO();
			BeanUtils.copyProperties(f.getFoodType(), foodTypeVO);
			foodVO.setFoodTypeVO(foodTypeVO);
			vo.add(foodVO);
		}
		return vo;
	}
	
	//查询全部菜系
	@Override
	public FoodAndType getFoodTypes() {
		List<FoodType> foodType = foodMapper.selectFoodType();
		List<FoodTypeVO> foodTypeVO = new ArrayList<FoodTypeVO>();
		FoodTypeVO type = null;
		for(FoodType foodTypes:foodType){
			type = new FoodTypeVO();
			BeanUtils.copyProperties(foodTypes, type);
			foodTypeVO.add(type);
		}
		FoodAndType vo = new FoodAndType(foodTypeVO);
		return vo;
	}
	
	//新增菜品
	@Override
	public void addFood(FoodVO foodVO) {
		Food food = new Food();
		BeanUtils.copyProperties(foodVO, food);
		FoodType foodType = new FoodType();
		foodType.setFoodtypeId(foodVO.getTypeId());
		food.setFoodType(foodType);
		foodMapper.addFood(food);
	
	}

	//显示详细菜品
	@Override
	public FoodVO getFoodById(Integer foodId) {
		Food entity = foodMapper.selectFoodById(foodId);
		if(entity!=null){
			FoodVO vo = new FoodVO();
			BeanUtils.copyProperties(entity, vo);			
			
		
			FoodTypeVO typeVO = new FoodTypeVO();
			BeanUtils.copyProperties(entity.getFoodType(), typeVO);
			vo.setFoodTypeVO(typeVO);	
			return vo;
			
		}
		return null;
	}
	//修改菜品
	@Override
	public void editFood(FoodVO foodVO) {
				Food food = new Food();
				BeanUtils.copyProperties(foodVO, food);
				FoodType foodType = new FoodType();
				foodType.setFoodtypeId(foodVO.getTypeId());
				food.setFoodType(foodType);
				foodMapper.editFood(food);
	}

	//删除菜品
	@Override
	public void deleteFood(Integer foodId) {
		foodMapper.deleteFood(foodId);
	}
	
	//根据菜系ID查询菜品
		@Override
		public List<FoodVO> searchFoodByFootType(Integer foodtypeId) {
			List<Food> list = foodMapper.searchFoodByFootType(foodtypeId);
			if(list!=null){
				FoodVO vo = null;
				List<FoodVO>  resultList=new ArrayList<FoodVO>();
				for(Food food : list){
					vo=new FoodVO();
					BeanUtils.copyProperties(food, vo);
					resultList.add(vo);
					
					FoodTypeVO typeVO = new FoodTypeVO();
					BeanUtils.copyProperties(food.getFoodType(), typeVO);
					vo.setFoodTypeVO(typeVO);						
					
				}
				return resultList;		
			}
			return null;
		}
}
